package searchTree;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-02-28
 * Time:16:16
 */
class Node{
    public int val;
    public Node left;
    public Node right;
    public Node(int val){
        this.val = val;
    }
}
public class BinarySearchTree {
    public Node root = null;//根节点
    public Node search(int key){
        Node cur = root;
        while(cur != null){
            if(cur.val < key){
                cur = cur.right;
            }else if(cur.val == key){
                return cur;
            }else{
                cur = cur.left;
            }
        }
        return null;//没有这个节点
    }
    public void insert(int val){
        if(root == null){
            root = new Node(val);
            return;
        }
        Node cur = root;
        Node parent = null;
        while(cur != null){
            parent = cur;
            if(cur.val < val){
                cur = cur.right;
            }else if(cur.val == val){
                return;//不能有两个相同的节点
            }else{
                cur = cur.left;
            }
        }
        //找到应插入的位置
        if(parent.val < val){
            parent.right = new Node(val);
        }else{
            parent.left = new Node(val);
        }
    }
    //删除
    public void remove(int key){
        if(root == null){
            return;
        }
        Node cur = root;
        Node parent = null;
        while(cur != null){
            parent = cur;
            if(cur.val < key){
                cur = cur.right;
            }else if(cur.val == key){
                removeNode(cur,parent);
            }else{
                cur = cur.left;
            }
        }
    }
    //cur 为要删除的节点 parent为其父亲节点
    public  void removeNode(Node cur,Node parent){
        if(cur.left == null){//要删除的节点左边为空
            if(cur == root){
                root = cur.right;
            }else if(cur == parent.left){//要删除的在左边
                parent.left = cur.right;
            }else{//要删除的在右边
                parent.right = cur.right;
            }
        }else if(cur.right == null){//右边为空
            if(cur == root){
                root = cur.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{//左边和右边都不为空
            Node ta = cur.right;
            Node tap = cur;
            while(ta.left != null){//找到一个可替换的叶子结点
                tap = ta;
                ta = ta.left;
            }
            cur.val = ta.val;//然后交换
            if(ta == tap.left){//此时就应该删除找到的这个节点，如果是左边的ta的left为空就应tap.left = ta.right
                tap.left = ta.right;
            }else{//如果是右边的话，ta的left为空，就应tap.left = ta.right
                tap.right = ta.right;
            }
        }
    }
    public void inOrder(Node root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public static void main(String[] args) {
        int[] array = {9,6,7,5,8,2,3,1};
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        for(int i = 0;i < array.length;i ++){
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();
        System.out.println("删除这个节点");
        binarySearchTree.remove(2);
        binarySearchTree.inOrder(binarySearchTree.root);

    }

}
